package com.lq.ecoldchain.binding.picker

import android.arch.lifecycle.MutableLiveData
import android.databinding.BindingAdapter
import android.view.View
import android.widget.TextView
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.lq.ecoldchain.callback.SafeClickListener
import com.lq.ecoldchain.util.GsonUtils
import java.util.*
import com.lq.ecoldchain.entity.JsonBean
import com.google.gson.Gson
import org.json.JSONArray
import kotlin.collections.ArrayList


/*
*2019/1/2 0002  17:41
*function by lq
*/
@BindingAdapter("bind_time_picker")
fun pickerAll(view: TextView, time: MutableLiveData<String>) {
    view.text = time.value
    view.setOnClickListener {
        TimePickerBuilder(view.context, OnTimeSelectListener { date, _ ->
            val calendar = Calendar.getInstance()
            calendar.time = date
            val month = calendar.get(Calendar.MONTH) + 1
            val day = calendar.get(Calendar.DAY_OF_MONTH)
            val trueMonth = if(month>9){"$month"}else{"0$month"}
            val trueDay = if(day>9){"$day"}else{"0$day"}
            time.value = "${calendar.get(Calendar.YEAR)}-$trueMonth-$trueDay"
            view.text = time.value
        }).build().show()
    }
}

@BindingAdapter("bind_picker_year")
fun pickerYear(view: TextView, time: MutableLiveData<String>) {
    view.text = time.value
    view.setOnClickListener {
        TimePickerBuilder(view.context, OnTimeSelectListener { date, _ ->
            val calendar = Calendar.getInstance()
            calendar.time = date
            time.value = "${calendar.get(Calendar.YEAR)}"
            view.text = time.value
        }).setType(booleanArrayOf(true, false, false, false, false, false)).build().show()
    }
}

@BindingAdapter("bind_city_all")
fun cityPicker(view: TextView, city: MutableLiveData<String>) {
    view.setOnClickListener(object : SafeClickListener() {
        override fun onNoDoubleClick(v: View) {
            val jsonString = GsonUtils.getJson(view.context, "province.json")
            val arrayJson = parseData(jsonString)
            val cityList = ArrayList<String>()
            for (i in 0 until arrayJson.size) {
                cityList.add(arrayJson[i].name)//所有省份城市
            }
            val picker = OptionsPickerBuilder(v.context, OnOptionsSelectListener { options1, _, _, _ ->
                city.value = cityList[options1]
                view.text = city.value
            }).build<String>()
            picker.setPicker(cityList)
        }
    })
}

fun parseData(result: String): ArrayList<JsonBean> {//Gson 解析
    val detail = ArrayList<JsonBean>()
    try {
        val data = JSONArray(result)
        for (i in 0 until data.length()) {
            val entity = Gson().fromJson(data.optJSONObject(i).toString(), JsonBean::class.java)
            detail.add(entity)

        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return detail
}


